package syntaxtext;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.*;
import tab.EyelashTab;
import org.fife.ui.rsyntaxtextarea.AbstractTokenMakerFactory;
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
import org.fife.ui.rsyntaxtextarea.Style;
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
import org.fife.ui.rsyntaxtextarea.SyntaxScheme;
import org.fife.ui.rsyntaxtextarea.Token;
import org.fife.ui.rsyntaxtextarea.TokenMakerFactory;
import org.fife.ui.rtextarea.RTextScrollPane;

public class SyntaxText {

    private RSyntaxTextArea editor = new RSyntaxTextArea();
    private RTextScrollPane scrollPane;
    private boolean change = true;//falso no se ha modificado    
    private String nameFile = "";
    private String pathFile = "";
    EyelashTab pestana;

    public SyntaxText(String file) {
        editor = new RSyntaxTextArea();
        editor.setPopupMenu(null);
        editor.setSyntaxEditingStyle(null);
        editor.setWrapStyleWord(true);
        //editor.setLineWrap(true);
        editor.setCaretPosition(0);
        editor.setCodeFoldingEnabled(true);
        editor.setAntiAliasingEnabled(true);
        editor.addKeyListener(keyListener());
        if (file.endsWith(".config") || file.endsWith(".agile")) {
            editor.setSyntaxEditingStyle(SyntaxConstants.SYNTAX_STYLE_C);
        }
        //----------------------------------------------------------------------
        SyntaxScheme scheme = editor.getSyntaxScheme();
        Font baseFont = RSyntaxTextArea.getDefaultFont();
        //scheme.setStyle(Token.FUNCTION, new Style(Color.DARK_GRAY, null, baseFont));
        //scheme.setStyle(Token.LITERAL_STRING_DOUBLE_QUOTE, new Style(new Color(206, 123, 0), null, baseFont));

        //scheme.getStyle(Token.RESERVED_WORD).background = Color.pink;
        //scheme.getStyle(Token.DATA_TYPE).foreground = Color.blue;
        //scheme.getStyle(Token.LITERAL_STRING_DOUBLE_QUOTE).underline = true;
        //scheme.getStyle(Token.COMMENT_EOL).font = new Font("Georgia", Font.ITALIC, 12);
        scrollPane = new RTextScrollPane(editor);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension scrollPaneSize = new Dimension(5 * screenSize.width / 8, 5 * screenSize.height / 8);
        scrollPane.setPreferredSize(scrollPaneSize);

        //PopupSyntaxText popup = new PopupSyntaxText(editor);
        //editor.setPopupMenu(popup.getPopup());
        //init();
    }

    public void createPopup() {
    }

    private void init() {
        ((AbstractTokenMakerFactory) TokenMakerFactory.getDefaultInstance()).putMapping(AgilelTokenMaker.StyleName,
                AgilelTokenMaker.class.getName());
        editor.setSyntaxEditingStyle(AgilelTokenMaker.StyleName);
        //editor.setSyntaxScheme(getWikiSyntaxScheme(editor));
        editor.setWrapStyleWord(true);
        editor.setLineWrap(true);
        editor.setCaretPosition(0);

    }

    public KeyListener keyListener() {
        KeyListener key = new KeyListener() {
            public void keyPressed(KeyEvent keyEvent) {
                change = true;
                pestana.filemodify();
            }

            public void keyReleased(KeyEvent keyEvent) {
                change = true;
                pestana.filemodify();
            }

            public void keyTyped(KeyEvent keyEvent) {
                change = true;
                pestana.filemodify();
            }
        };
        return key;
    }

    public RTextScrollPane getScrollPane() {
        return scrollPane;
    }

    public void selectAll() {
        editor.selectAll();
    }

    public void fileNew() {
        change = false;
        nameFile = "";
        pathFile = "";
        editor.setText("");
        scrollPane.setViewportView(editor);
        editor.requestFocus();
        editor.setCaretPosition(0);
    }

    public void setText(String text) {
        editor.setText(text);
        editor.setCaretPosition(0);
        scrollPane.setViewportView(editor);
    }

    public String getText() {
        return editor.getText();
    }

    public boolean isChange() {
        return change;
    }

    public void setChange(boolean change) {
        this.change = change;
    }

    public String getNameFile() {
        return nameFile;
    }

    public void setNameFile(String nameFile) {
        this.nameFile = nameFile;
    }

    public String getPathFile() {
        return pathFile;
    }

    public void setPathFile(String pathFile) {
        this.pathFile = pathFile;
    }

    public void setEyelash(EyelashTab pestana) {
        this.pestana = pestana;
    }

    public class AccionMenu extends AbstractAction {

        private String textoOpcion;

        /**
         * Se le pasa el nombre que se quiere que se muestre
         *
         * @param textoOpcion
         */
        public AccionMenu(String textoOpcion) {
            this.textoOpcion = textoOpcion;
            this.putValue(Action.NAME, textoOpcion);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            System.out.println("Pulsado " + textoOpcion);
            editor.append("textoOpcion");
        }
    }
}
